
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen">
        <!-- <template slot-scope="scope" slot="handleTypeForm">
          <avue-select v-model="form.handleType" placeholder="请选择办卡类型" type="tree" :dic="handleTypeDic"></avue-select>
        </template> -->

        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="readCard">读卡</el-button>
          <el-button type="primary" size="small" @click="delBatch">删除</el-button>
          <el-upload
            class="upload-inline"
            action
            :show-file-list="false"
            accept=".xls, .xlsx"
            :http-request="handleImport"
          >
            <el-button type="primary" size="small">导入</el-button>
          </el-upload>
          <el-button type="primary" size="small" @click="exportClick">导出</el-button>
        </template>
        <template slot="menuRight">
          <el-button
            @click="handleDownloadClick"
            type="text"
            size="small"
          >模板下载</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  delsObj,
  saveBatch,
  dataExport,templateDowload
} from "@/api/subway/swtkofficialticket";
import { startDate, endDate } from "@/const/crud/subway/swtkofficialticket";
import { dateFormat } from '@/util/date.js'
import { handleExcelResponse } from "@/util/util";

export default {
  name: "swtkofficialticket",
  mixins: [baseCrud('subway/swtkofficialticket')],
  data() {
    return {
      formType: "",
      searchForm: {
        //dates1: [startDate, endDate]
      },
      dicAdd: [
        {
          label: "申办新卡",
          value: "0"
        },{
          label: "修改",
          value: "1"
        },
        {
          label: "续期",
          value: "2"
        },
        {
          label: "退卡",
          value: "3"
        },
        {
          label: "遗失补办",
          value: "4"
        },
        {
          label: "非人为损坏补办",
          value: "5"
        },
        {
          label: "人为损坏补办",
          value: "6"
        }
      ],
      dicEdit: [
        {
          label: "申办新卡",
          value: "0",
          disabled: true
        },
        {
          label: "修改",
          value: "1"
        },
        {
          label: "续期",
          value: "2"
        },
        {
          label: "退卡",
          value: "3"
        },
        {
          label: "遗失补办",
          value: "4"
        },
        {
          label: "非人为损坏补办",
          value: "5"
        },
        {
          label: "人为损坏补办",
          value: "6"
        }
      ]
    };
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.st_swtkofficialticket_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.st_swtkofficialticket_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.st_swtkofficialticket_edit,
          false
        )
      };
    },
    handleTypeDic(){
      if(this.formType == 'add'){
        return this.dicAdd
      }else{
        return this.dicEdit
      }
    }
  },
  methods: {
    async handleDownloadClick () {//下载导出模板
      this.uploading = true;
      try {
        const res = await templateDowload();
        handleExcelResponse(res);
      } finally {
        this.uploading = false;
      }
    },
    beforeRowSave(row,done, loading){
      if(row.officialType == "0"){
        if(this.validatenull(row.lineCode)){
          this.$message.info("线路不能为空！");
          loading();
          return false;
        }
        if(this.validatenull(row.stationCode)){
          this.$message.info("车站不能为空！");
          loading();
          return false;
        }
      }
      done();
    },
    beforeRowUpdate(row,index,done, loading){
      if(row.officialType == "0"){
        if(this.validatenull(row.lineCode)){
          this.$message.info("线路不能为空！");
          loading();
          return false;
        }
        if(this.validatenull(row.stationCode)){
          this.$message.info("车站不能为空！");
          loading();
          return false;
        }
      }
      done();
    },
    handleImport(item) {
      let file = item.file;
      this.$message.info("正在导入，请耐心等待！");
      let param = new FormData();
      param.append("file", file);
      saveBatch(param).then(() => {
        this.$notify({
          title: "成功",
          message: "导入成功",
          type: "success",
          duration: 2000
        });
        this.getList(this.page);
      });
    },
    delBatch() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("是否确认删除","提示",{
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() =>{
        delsObj(ids)
          .then(data => {
            this.$message.success("删除成功");
            this.getList(this.page);
          })
          .catch(() => {
            //loading();
          });
      })
    },
    beforeOpen(done, type) {
      if (["view", "edit"].includes(type)) {
        // 查看和编辑逻辑
        setTimeout(() => {
          this.formType = "edit";
        }, 0);
      } else {
        //新增逻辑
        setTimeout(() => {
          this.formType = "add";
          let now = new Date()
          this.form.sendCardTime = dateFormat(now).substr(0, 10)
        }, 0);
      }
      done();
    },

    async exportClick (row) {
      this.dataExport();
    },
    async dataExport () {
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await dataExport(Object.assign( this.searchForm ));
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
  }
};
</script>
